<?php
require_once '../kelas/kategori.php';
$objKategori = new kategori();
$secure = new amankanParam();
$getMethod = $secure->getMethod();
?>
<h3>Kategori</h3>
<form  enctype="multipart/form-data" name="formKategori" method="POST" >
    <input type="hidden" name="id" />
    <label >Nama Kategori :<input required="required" placeholder="Nama Kategori" type="text" class="text_box_3" name="nama" /></label>
    <br />
    <label>Aktif<input type="checkbox" checked="checked" name="aktif" value="1"></label>
    <br />
    <input type="submit" name="submit" value="Simpan" />
    <input type="reset" value="Reset" />
</form>
<div class="frame_table_order">
<table width="100%" cellpadding="5" cellspacing="0" border="1" style="border-collapse:collapse">
    <thead>
    <tr align="center" valign="middle" bgcolor="#CCCCCC">
            <th><span class="font_sub_judul_2">No</span></th>
            <th><span class="font_sub_judul_2">Nama</span></th>
            <th><span class="font_sub_judul_2">Aktif</span></th>
            <th>Pilihan</th>            
        </tr>
    </thead>
    <tbody>
        <?php
        $fung = new fungsi();
        $dtPerPage = 3;
        $start = isset($getMethod['hal']) ? $getMethod['hal'] : 0;
        //$start = $dtPerPage * $limit;
        $dataKat = $objKategori->getData($dtPerPage, $start * $dtPerPage);
        $no = 1 + ($start * $dtPerPage); //nanti bisa dinamis
        foreach ($dataKat['result'] as $vKat) {
            ?>
            <tr class="font_isi_judul" align="center" valign="middle">

            <td><?php echo $no++; ?></td>
                <td><?php echo $vKat['nama']; ?></td>
                <td><span class="font_sub_judul_2"><a href="#" class="<?php echo ($vKat['aktif'] == 1) ? "iconActive" : "iconNonaktif"; ?>">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a></span></td>
                <td><span class="font_sub_judul_2"><a href="#" 
                       nama="<?php echo $vKat['nama'] ?>" aktif="<?php echo $vKat['aktif'] ?>" 
                       id_kategori="<?php echo $vKat['id'] ?>" 
                       class="aktifasi_kategori option"><?php echo ($vKat['aktif'] == 1) ? "Nonaktifkan" : "Aktifkan"; ?></a>
                    <a href="#"
                       nama="<?php echo $vKat['nama'] ?>" aktif="<?php echo (int) $vKat['aktif'] ?>" 
                       id_kategori="<?php echo $vKat['id'] ?>"                        
                       class="edit_kategori option">Edit</a></span></td>
            </tr><?php
    }
        ?>
    </tbody>
    <tfoot>
        <tr><th colspan="4"><br/><?php
        echo $fung->buatHalaman($dataKat['total'], $dtPerPage, $start, '&v=master&p=kategori')
        ?><br/></th></tr>
    </tfoot>
</table>
</div>
<script type="text/javascript">
    $(document).ready(function(){
        $(document).find('form[name=formKategori]').find('input[type=reset]').click(function(e){
            var thisForm = $(document).find('form[name=formKategori]');
            thisForm.find('input[type=submit]').val('Simpan');            
        });
        $(document).find('form[name=formKategori]').submit(function(e){
            var thisForm = $(document).find('form[name=formKategori]');
            $.ajax({
                dataType:'JSON',
                url:baseUrl+'proses/kategori.php?on=simpan',
                type:thisForm.attr('method'),
                data:thisForm.serialize(),
                success:function(e){
                    document.location = document.URL;
                    //document.location=document.URL;
                },
                error:function(e){                   
                    console.log(e);
                    alert('Ada kesalahan dalam memproses:');
                }
            });
            e.preventDefault();
        });
        $('a.aktifasi_kategori').click(function(e){
            var thisLink = e.target.attributes;
            var idKat = thisLink.getNamedItem('id_kategori').value;
            var statAkt = thisLink.getNamedItem('aktif').value; 
            $.ajax({
                url:baseUrl+'proses/kategori.php?on=simpan',
                type:'POST',
                data:{
                    id:idKat,
                    aktif:statAkt=="0"?1:0
                },
                success:function(e){
                    document.location = document.URL;
                },
                error:function(e){
                    alert('Gagal Ubah Status Kategori');
                }
            });
            e.preventDefault();
        });
        $('a.edit_kategori').click(function(e){
            thisKat = this.attributes;
            var formKat  = $(document).find('form[name=formKategori]');
            var nama = thisKat.getNamedItem('nama').value;
            var aktif= thisKat.getNamedItem('aktif').value;
            var idKat= thisKat.getNamedItem('id_kategori').value;
            formKat.find('input[name=nama]').val(nama);
            formKat.find('input[name=id]').val(idKat);
            formKat.find('input[type=submit]').val("Update");
            formKat.find('input[name=aktif]').removeAttr('checked');
            if(aktif=='1'){
                formKat.find('input[name=aktif]').click();
            }            
            e.preventDefault();
        })
    });
    
</script>
